package config

import (
	"encoding/json"
	"os"
)

type tRaftConfig struct {
	ID    string
	Nodes []*tRaftNodeConfig
}

type tRaftNodeConfig struct {
	ID       string
	Endpoint string
}

func (me *tRaftConfig) GetID() string {
	return me.ID
}

func (me *tRaftConfig) GetNodes() []IRaftNodeConfig {
	a := make([]IRaftNodeConfig, len(me.Nodes))
	for i, it := range me.Nodes {
		a[i] = it
	}
	return a
}

func (me *tRaftNodeConfig) GetID() string {
	return me.ID
}

func (me *tRaftNodeConfig) GetEndpoint() string {
	return me.Endpoint
}

func LoadJSONFile(file string) IRaftConfig {
	data, err := os.ReadFile(file)
	if err != nil {
		panic(err)
	}

	c := new(tRaftConfig)
	err = json.Unmarshal(data, c)
	if err != nil {
		panic(err)
	}

	return c
}
